package com.itheima.ssm.controller;

import com.itheima.ssm.domain.Product;
import com.itheima.ssm.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 查询所有产品信息
     * @return
     * @throws Exception
     */
    @Secured({"ROLE_ADMIN"})
    @RequestMapping("/findAll")
    public ModelAndView findAll() throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Product> pd = productService.findAll();
        mv.addObject("productList",pd);
        mv.setViewName("product-list");
        return mv;
    }

    /**
     * 添加产品
     * @return
     */
    @RequestMapping("/save")
    public String saveProduct(Product product){
        //调用service方法
        productService.save(product);
        //跳转到查询所有产品页面
        return "redirect:/product/findAll";
    }
}
